//
//  ProfileEditingViewController.m
//  ContactManager
//
//  Created by mzhou19 on 11/2/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ProfileEditingViewController.h"


@implementation ProfileEditingViewController
@synthesize nameField;
@synthesize numberField;
@synthesize addressField;
@synthesize emailField;
@synthesize message;
@synthesize imageOfContact;
@synthesize loadimage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.title = @"Edit Profile";
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *Name = [message objectForKey:@"name"];
    NSString *PhoneNumber = [message objectForKey:@"phone number"];
    NSString *Address = [message objectForKey:@"home address"];
    NSString *Email = [message objectForKey:@"email"];
    NSString *image = [message objectForKey:@"image"];
    self.imageOfContact.image = [UIImage imageNamed:image];
    self.nameField.text = Name;
    self.numberField.text = PhoneNumber;
    self.addressField.text = Address;
    self.emailField.text = Email;
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                              target:self 
                                                                              action:@selector(doneButtonPressed)];
	[[self navigationItem] setRightBarButtonItem:doneItem];
	[doneItem release];
}



- (void) doneButtonPressed {
    NSString *test = @"-1";
    [message setValue:self.nameField.text forKey:@"name"];
    [message setValue:self.numberField.text forKey:@"phone number"];
    [message setValue:self.addressField.text forKey:@"home address"];
    [message setValue:self.emailField.text forKey:@"email"];
    //check the status of note. new one or old one
    if ([message valueForKey:@"index"] == test) {
        [message setValue:@"1" forKey:@"index"];
        [[RecordManager sharedManager] saveNewRecords:message];
        [self.navigationController popViewControllerAnimated:YES];
    }    
    
    [[RecordManager sharedManager] saveRecords];
    
    [super.navigationController popToRootViewControllerAnimated:YES];
}

-(IBAction)textFieldDone:(id)sender {
    [sender resignFirstResponder];
}


-(IBAction)loadimageview {
    testPickerViewController *detailViewController = [[testPickerViewController alloc] initWithNibName:@"testPickerViewController" bundle:nil];
    [self.navigationController pushViewController:detailViewController animated:YES];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
